package com.cloudsigns.factorymanager2.engineering.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.GridView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.IView.IEngineeringFeedBackReplyForMnagerView;
import com.cloudsigns.factorymanager2.engineering.IView.IEngineeringInstallDailyFeedbackConfirmView;
import com.cloudsigns.factorymanager2.engineering.IView.IFeedBackDetailView;
import com.cloudsigns.factorymanager2.engineering.adapter.GridAdapter;
import com.cloudsigns.factorymanager2.engineering.bean.DailyFeedBackStatuBean;
import com.cloudsigns.factorymanager2.engineering.bean.EngineeringDailyFeedBackBean;
import com.cloudsigns.factorymanager2.engineering.bean.EngineeringInstallStatuBean;
import com.cloudsigns.factorymanager2.engineering.presenter.EngineeringFeedBackReplyForMnagerPresenter;
import com.cloudsigns.factorymanager2.engineering.presenter.EngineeringInstallDailyFeedbackConfirmPresenter;

import com.cloudsigns.factorymanager2.engineering.presenter.FeedBackDetailPresenter;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;
import com.cloudsigns.factorymanager2.project.utils.DateUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by tony on 2018/5/30.
 */

public class FeedBackSureForManagerActivity extends Activity implements IEngineeringInstallDailyFeedbackConfirmView, IFeedBackDetailView, IEngineeringFeedBackReplyForMnagerView, View.OnClickListener {
    private RelativeLayout rl_back;
    private TextView tv_title;
    private TextView edt_feed_back_type, edt_exception_type, edt_unusual_reason, edt_advice, edt_impact, edt_feedback_people_colon, edt_feed_back_time;
    private TextView btn_confirm;
    private GridView gridView;
    private GridAdapter gridAdapter;
    private List<String> imgUrlList = new ArrayList<>();
    private View rl_replay_content, rl_confirm_person, rl_confirm_time;
    private TextView edt_replay_content, edt_confirm_person, edt_confirm_time;
    private View rl_feedback_people_colon,rl_feed_back_time;
    // private ProduceOrderParentBean orderParentBean;
//    private EngineeringInstallOrderDetailPresenter engineeringInstallOrderDetailPresenter;
//    private EngineeringInstallationOrderBean engineeringInstallationOrderBean;
//    private List<EngineeringDailyFeedBackBean> engineeringDailyFeedBackBeanList;
    private EngineeringDailyFeedBackBean engineeringDailyFeedBackBean;
    private String orderId, dailyFeedbackId;
    private CommonEnum.EngineeringORderCategory type;
    private EngineeringInstallDailyFeedbackConfirmPresenter engineeringInstallDailyFeedbackConfirmPresenter;
    private FeedBackDetailPresenter feedBackDetailPresenter;
    private EngineeringFeedBackReplyForMnagerPresenter engineeringFeedBackReplyForMnagerPresenter;
    private String role = "";
    private DailyFeedBackStatuBean dailyFeedBackStatuBean;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        // 沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
            );
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN

                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);

        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        setContentView(R.layout.layout_feedback_sure_for_super);


//        engineeringInstallOrderDetailPresenter = new EngineeringInstallOrderDetailPresenter(this);
        engineeringInstallDailyFeedbackConfirmPresenter = new EngineeringInstallDailyFeedbackConfirmPresenter(this);
        feedBackDetailPresenter = new FeedBackDetailPresenter(this);
        engineeringFeedBackReplyForMnagerPresenter = new EngineeringFeedBackReplyForMnagerPresenter(this);
        init();

        Intent mIntent = getIntent();
        if (null != mIntent) {
            String tag = mIntent.getStringExtra(ConstantUtil.KEY_1);
            if (!TextUtils.isEmpty(tag)) {
                if (tag.equals("EngineeringInstallFeedBackForWaitStateActivity")) {
                    orderId = mIntent.getStringExtra(ConstantUtil.KEY_2);
                    engineeringDailyFeedBackBean = (EngineeringDailyFeedBackBean) mIntent.getSerializableExtra("feedbackbean");
                    role = mIntent.getStringExtra("role");

                    if (null != engineeringDailyFeedBackBean)
                        dailyFeedbackId = engineeringDailyFeedBackBean.getId();

                    type = CommonEnum.EngineeringORderCategory.INSTALL;

                } else if (tag.equals("EngineeringInstallStatusTableForManagerActivity1")) {
                    dailyFeedBackStatuBean = (DailyFeedBackStatuBean) mIntent.getSerializableExtra(ConstantUtil.KEY_2);
                    if (null != dailyFeedBackStatuBean) {
                        dailyFeedbackId = dailyFeedBackStatuBean.getDailyFeedbackId();
                    }

                    EngineeringInstallStatuBean engineeringInstallStatuBean = (EngineeringInstallStatuBean) mIntent.getSerializableExtra("EngineeringInstallStatuBean");
                    if (null != engineeringInstallStatuBean) {
                        orderId = engineeringInstallStatuBean.getOrderId();
                    }

                    type = CommonEnum.EngineeringORderCategory.INSTALL;


                    feedBackDetailPresenter.feedBackDetail();
                }
            }
//            engineeringDailyFeedBackBean = (EngineeringDailyFeedBackBean) mIntent.getSerializableExtra(ConstantUtil.KEY_1);
//            orderId = mIntent.getStringExtra(ConstantUtil.KEY_2);
//            orderParentBean = (ProduceOrderParentBean) mIntent.getSerializableExtra(ConstantUtil.KEY_1);
        }


//        engineeringInstallOrderDetailPresenter.getEngineeringInstallOrderDetail();
        initData();
    }

    private void init() {
        rl_back = findViewById(R.id.rl_back);
        tv_title = findViewById(R.id.tv_title);

        edt_feed_back_type = findViewById(R.id.edt_feed_back_type);
        edt_exception_type = findViewById(R.id.edt_exception_type);
        edt_unusual_reason = findViewById(R.id.edt_unusual_reason);
        edt_advice = findViewById(R.id.edt_advice);
        edt_impact = findViewById(R.id.edt_impact);
        edt_feedback_people_colon = findViewById(R.id.edt_feedback_people_colon);
        edt_feed_back_time = findViewById(R.id.edt_feed_back_time);
        rl_confirm_person = findViewById(R.id.rl_confirm_person);
        rl_confirm_time = findViewById(R.id.rl_confirm_time);
        edt_confirm_person = findViewById(R.id.edt_confirm_person);
        edt_confirm_time = findViewById(R.id.edt_confirm_time);
        edt_replay_content = findViewById(R.id.edt_replay_content);
        rl_replay_content = findViewById(R.id.rl_replay_content);
        rl_feedback_people_colon = findViewById(R.id.rl_feedback_people_colon);
        rl_feed_back_time = findViewById(R.id.rl_feed_back_time);


        gridView = findViewById(R.id.gridView);
        gridView.setNumColumns(3);
        gridAdapter = new GridAdapter(this);
        gridAdapter.setData(imgUrlList);
        gridView.setAdapter(gridAdapter);

        btn_confirm = findViewById(R.id.btn_confirm);


        tv_title.setText("工程反馈");

        rl_back.setOnClickListener(this);
        btn_confirm.setOnClickListener(this);


    }


    private void initData() {

        if (null != engineeringDailyFeedBackBean) {
            CommonEnum.FeedBackType feedBackType = engineeringDailyFeedBackBean.getType();
            if (null != feedBackType) {
                if (feedBackType == CommonEnum.FeedBackType.NORMAL) {
                    edt_feed_back_type.setText("正常反馈");

                    CommonEnum.NormalSubType normalSubType = engineeringDailyFeedBackBean.getNormalType();
                    if (null != normalSubType) {
                        if (normalSubType == CommonEnum.NormalSubType.CONSTRUCT) {
                            edt_exception_type.setText("工程");
                        } else if (normalSubType == CommonEnum.NormalSubType.LOGISTICS) {
                            edt_exception_type.setText("物流");

                        }
                    }

                    String contentdesc = engineeringDailyFeedBackBean.getContentDesc();
                    if (null != contentdesc && !TextUtils.isEmpty(contentdesc) && !"null".equals(contentdesc)) {
                        edt_unusual_reason.setText(contentdesc);
                    }

                    String status = engineeringDailyFeedBackBean.getStatus();
                    if (null != status && !TextUtils.isEmpty(status) && !"null".equals(status)) {
                        edt_unusual_reason.setText(status);
                    }

                } else if (feedBackType == CommonEnum.FeedBackType.UNUSUAL) {
                    edt_feed_back_type.setText("异常反馈");
                } else if (feedBackType == CommonEnum.FeedBackType.COMPLETED) {
                    edt_feed_back_type.setText("完工反馈");
                }
            }

            CommonEnum.SubType subType = engineeringDailyFeedBackBean.getSubType();
            if (null != subType) {
                if (subType == CommonEnum.SubType.DELAY) {
                    edt_exception_type.setText("物流延误");
                    edt_unusual_reason.setText("延误天数：" + engineeringDailyFeedBackBean.getDelayDays());
                } else if (subType == CommonEnum.SubType.DAMAGE) {
                    edt_exception_type.setText("物流货损");
                    edt_unusual_reason.setText(engineeringDailyFeedBackBean.getDamageProduct() + "：" + engineeringDailyFeedBackBean.getDamageNumber());
                } else if (subType == CommonEnum.SubType.INSTALL_UNUSUAL) {
                    edt_exception_type.setText("安装异常");
                    edt_unusual_reason.setText(engineeringDailyFeedBackBean.getUnusualReason());
                } else if (subType == CommonEnum.SubType.OMIT) {
                    edt_exception_type.setText("物流漏货");
                    edt_unusual_reason.setText(engineeringDailyFeedBackBean.getOmitContent() + "：" + engineeringDailyFeedBackBean.getOmitNumber());
                }
            }

            String advice = engineeringDailyFeedBackBean.getAdvice();
            if (null != advice && !TextUtils.isEmpty(advice) && !"null".equals(advice))
                edt_advice.setText(engineeringDailyFeedBackBean.getAdvice());

            edt_impact.setText(engineeringDailyFeedBackBean.getUnusualDays() + "天");

            edt_feedback_people_colon.setText(engineeringDailyFeedBackBean.getAccountName());
            edt_feed_back_time.setText(DateUtil.getYyyy_Mm_Dd(engineeringDailyFeedBackBean.getFeedbackDate()));


            List<String> list = engineeringDailyFeedBackBean.getImgUrlList();
            if (null != list) {
                imgUrlList.addAll(list);
                gridAdapter.setData(imgUrlList);
                gridAdapter.notifyDataSetChanged();
            }

            if (!TextUtils.isEmpty(role) && role.equals("super"))
                btn_confirm.setVisibility(View.GONE);
            else {

                String handlerAccountId = engineeringDailyFeedBackBean.getHandlerAccountId();
                if (null != feedBackType) {
                    if (feedBackType == CommonEnum.FeedBackType.NORMAL) {
                        btn_confirm.setVisibility(View.GONE);
//                        if (null != handlerAccountId && !TextUtils.isEmpty(handlerAccountId) && !handlerAccountId.equals("null")) {
//                            btn_confirm.setVisibility(View.GONE);
////                            showHandleView();
//                        } else {
//                            btn_confirm.setVisibility(View.VISIBLE);
                    } else if (feedBackType == CommonEnum.FeedBackType.UNUSUAL) {
                        CommonEnum.UnusualStatus unusualStatus = engineeringDailyFeedBackBean.getUnusualStatus();
                        if (null != unusualStatus) {
                            if (unusualStatus == CommonEnum.UnusualStatus.WAIT_CONFIRM) {
                                btn_confirm.setVisibility(View.VISIBLE);
                            } else if (unusualStatus == CommonEnum.UnusualStatus.WAIT_REPLY) {
                                btn_confirm.setVisibility(View.GONE);
//                                showHandleView();

                            } else if (unusualStatus == CommonEnum.UnusualStatus.WAIT_REPLY_CONFIRM) {
                                btn_confirm.setVisibility(View.VISIBLE);
                                showReplayView();
                            } else if (unusualStatus == CommonEnum.UnusualStatus.REPLY_CONFIRM_FINISH) {
                                btn_confirm.setVisibility(View.GONE);

                            }
                        }
                    } else if (feedBackType == CommonEnum.FeedBackType.COMPLETED) {
                        btn_confirm.setVisibility(View.GONE);
//                        btn_confirm.setVisibility(View.VISIBLE);
                    }
                }

            }


        }
    }


//    private void showHandleView() {
//        rl_confirm_person.setVisibility(View.VISIBLE);
//        rl_confirm_time.setVisibility(View.VISIBLE);
//
//        rl_replay_content.setVisibility(View.GONE);
//        rl_confirm_person.setVisibility(View.GONE);
//        rl_confirm_time.setVisibility(View.GONE);
//        if (null != engineeringDailyFeedBackBean) {
//            String handlerAccountId = engineeringDailyFeedBackBean.getHandlerAccountId();
//            if (null != handlerAccountId && !TextUtils.isEmpty(handlerAccountId) && !handlerAccountId.equals("null")) {
//                edt_confirm_person.setText(handlerAccountId + ":" + engineeringDailyFeedBackBean.getHandlerAccountName());
//                edt_confirm_time.setText(DateUtil.getYyyy_Mm_Dd(engineeringDailyFeedBackBean.getHandleDate()));
//            }
//        }
//
//
//    }


    private void showReplayView() {
        rl_feedback_people_colon.setVisibility(View.GONE);
        rl_feed_back_time.setVisibility(View.GONE);


        rl_replay_content.setVisibility(View.VISIBLE);
        rl_confirm_person.setVisibility(View.VISIBLE);
        rl_confirm_time.setVisibility(View.VISIBLE);
        if (null != engineeringDailyFeedBackBean) {
            String completeUnusualAccountId = engineeringDailyFeedBackBean.getCompleteUnusualAccountId();
            if (null != completeUnusualAccountId && !TextUtils.isEmpty(completeUnusualAccountId) && !completeUnusualAccountId.equals("null")) {
                edt_replay_content.setText(engineeringDailyFeedBackBean.getCompleteUnusualDesc());
                edt_confirm_person.setText(engineeringDailyFeedBackBean.getConfirmCompleteUnusualAccountName());
                edt_confirm_time.setText(DateUtil.getYyyy_Mm_Dd(engineeringDailyFeedBackBean.getCompleteUnusualDate()));
            }
        }


    }

    private void feedbackConfirm() {
        if (null != engineeringDailyFeedBackBean) {
            CommonEnum.FeedBackType feedBackType = engineeringDailyFeedBackBean.getType();
            if (null != feedBackType) {
                if (feedBackType == CommonEnum.FeedBackType.UNUSUAL) {
                    CommonEnum.UnusualStatus unusualStatus = engineeringDailyFeedBackBean.getUnusualStatus();
                    if (null != unusualStatus) {
                        if (unusualStatus == CommonEnum.UnusualStatus.WAIT_CONFIRM) {
                            engineeringInstallDailyFeedbackConfirmPresenter.engineeringIstallDailyFeedbackConfirm();
                        } else if (unusualStatus == CommonEnum.UnusualStatus.WAIT_REPLY_CONFIRM) {
                            engineeringFeedBackReplyForMnagerPresenter.engineeringFeedBackRply();
                        }
                    }
                }
            }
        }


    }

    @Override
    public void onClick(View view) {
        Intent mIntent;
        switch (view.getId()) {
            case R.id.rl_back:
                finish();
                break;

            case R.id.btn_confirm:
                feedbackConfirm();
                break;


        }
    }


    @Override
    public String getcontent() {
        return null;
    }

    @Override
    public String getDailyFeedbackId() {

        return dailyFeedbackId;
    }

    @Override
    public String getOrderId() {
        return orderId;
    }


    @Override
    public CommonEnum.EngineeringORderCategory getType() {

        return type;
    }

    @Override
    public void feedBackDetailSuccessed(EngineeringDailyFeedBackBean dailyFeedBackBean) {
        this.engineeringDailyFeedBackBean = dailyFeedBackBean;
        initData();
    }

    @Override
    public void feedBackDetailFailed(String desc) {
        Toast.makeText(this, desc, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void feeedbackSuccessed() {
        Toast.makeText(this, "成功", Toast.LENGTH_SHORT).show();
        Intent mIntent = new Intent();
        setResult(ConstantUtil.START_ACTIVITY_KEY_1, mIntent);
        finish();
    }

    @Override
    public void showFailedError(String desc) {
        Toast.makeText(this, desc, Toast.LENGTH_SHORT).show();
    }


    @Override
    public String getContent() {
        return null;
    }


    @Override
    public void engineeringFeedBackRplySuccessed() {
        Toast.makeText(this, "成功", Toast.LENGTH_SHORT).show();
        Intent mIntent = new Intent();
        setResult(ConstantUtil.START_ACTIVITY_KEY_1, mIntent);
        finish();
    }

    @Override
    public void engineeringFeedBackRplyFailed(String desc) {
        Toast.makeText(this, desc, Toast.LENGTH_SHORT).show();
    }


    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }
}

